﻿using System;

namespace LoungeRepo.EntityCache
{
    partial class SecondLevelCache
    {
        private void Lock(Action execute, LockModes lockMode)
        {
            Lock(
                () =>
                {
                    execute();
                    return true;
                },
                lockMode
                );
        }

        private T Lock<T>(Func<T> execute, LockModes lockMode)
        {
            if (lockMode == LockModes.LockForRead)
                this.rwLock.AcquireReaderLock(500);
            else
                this.rwLock.AcquireWriterLock(500);
            try
            {
                return execute();
            }
            finally
            {
                if (lockMode == LockModes.LockForRead)
                    this.rwLock.ReleaseReaderLock();
                else
                    this.rwLock.ReleaseWriterLock();
            }
        }
    }
}
